package com.yksj.healthtalk.ui.app;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.universalimageloader.core.download.ImageDownloader;

import com.yksj.healthtalk.entity.BaseInfoEntity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.entity.MessageEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
/**
 * 全局变量缓存
 * @author zhao
 */
public class AppData {
	
	public static String ShareContent = "我在壹健康看到一条值得分享的内容:%1$s...更多内容查看：http://yijiankangv.mobi/SJ/download.html";
	/**
	 * 未读消息
	 */
	public final ConcurrentHashMap<String,List<MessageEntity>> messageCllection = new ConcurrentHashMap<String, List<MessageEntity>>();
	
	public final ConcurrentHashMap<String,MessageEntity> messageMap = new ConcurrentHashMap<String, MessageEntity>();//消息缓存
	public final ConcurrentHashMap<String,Object> cacheInformation = new ConcurrentHashMap<String,Object>();//用户或者群的资料信息
	private final ConcurrentHashMap<String,List<String>> typeListMap = new ConcurrentHashMap<String,List<String>>();//分类列表
	private final ConcurrentHashMap<String,MessageEntity> sendMesgeCache = new ConcurrentHashMap<String, MessageEntity>();//未发,发送失败消息缓存
	public void clearAll(){
		messageCllection.clear();
		cacheInformation.clear();
		typeListMap.clear();
		messageMap.clear();
		sendMesgeCache.clear();
		init();
	}
	
	public ConcurrentHashMap<String, MessageEntity> getSendMesgeCache() {
		return sendMesgeCache;
	}
	public void addSendMesgeCache(MessageEntity entity){
		sendMesgeCache.put(entity.getId(),entity);
	}

	public void removeSendMesgeCache(MessageEntity entity){
		sendMesgeCache.put(entity.getId(),entity);
	}

	public MessageEntity getSendCacheMessageEntity(String id){
		return sendMesgeCache.get(id);
	}
	

	public int attentionUserSize;
	public int blackListSize;
	//当前正在聊天的id
	public String currentChatId = null;
	//当前是否在主界面
	public boolean isAtMainUI = false;
	//底部消息切换时间
	public String messageShowTime ="5000";
	public AppData(){
		init();
	}
	
	private void init(){
		List latelyGroupList = Collections.synchronizedList(new ArrayList());//话题最近参与
		List interestGroupList = Collections.synchronizedList(new ArrayList());
		List createGroupList = Collections.synchronizedList(new ArrayList());//自己创建的话题
		List latelyFriendList = Collections.synchronizedList(new ArrayList());//最近联系
		List interestFriendList = Collections.synchronizedList(new ArrayList());//关注好友
		List fanFriendList = Collections.synchronizedList(new ArrayList());//粉丝
		List weibFriendList = Collections.synchronizedList(new ArrayList());//微博好友列表
		List customList = Collections.synchronizedList(new ArrayList());//客户
		List doctorList = Collections.synchronizedList(new ArrayList());//医生
		List blackList = Collections.synchronizedList(new ArrayList());//黑名单
		List strangerLis1t = Collections.synchronizedList(new ArrayList());//陌生人
		List doctorLatelyList = Collections.synchronizedList(new ArrayList());//最近医生 
		List customLatelyList = Collections.synchronizedList(new ArrayList());//最近客户
		List buyFailureGroupList = Collections.synchronizedList(new ArrayList());//支付失败的话题 
		List myBoughtGroupIdList = Collections.synchronizedList(new ArrayList());//我购买的话题
		List myBoughtHisGroupIdList = Collections.synchronizedList(new ArrayList());//我购买过的话题	
		
		typeListMap.put("1",latelyGroupList);//最近群聊
		typeListMap.put("2",interestGroupList);//关注的群
		typeListMap.put("3",createGroupList);//自己创建的群
		typeListMap.put("4",latelyFriendList);//最近好友列表
		typeListMap.put("5",interestFriendList);//关注的好友
		typeListMap.put("6",customLatelyList);//最近联系的客户
		typeListMap.put("7",doctorLatelyList);//最近医生
		typeListMap.put("8",blackList);
		typeListMap.put("9", customList);
		typeListMap.put("10", doctorList);
		typeListMap.put("11", buyFailureGroupList);
		typeListMap.put("12", myBoughtGroupIdList);
		typeListMap.put("13", myBoughtHisGroupIdList);
		typeListMap.put("14",strangerLis1t);
		CustomerInfoEntity entity = new CustomerInfoEntity();
		entity.setId(SmartFoxClient.helperId);
		entity.setName("多美助理");
		entity.setSex("2");
		entity.setNormalHeadIcon(ImageDownloader.PROTOCOL_FILE_ASSETS + ":///customerIcons/icon_healper.png");
		entity.setDescription("我是多美助理");
		cacheInformation.put(entity.getId(),entity);
	}
	
	public List<CustomerInfoEntity> getChatList(String tag){
		List<CustomerInfoEntity> infoEntities = new ArrayList<CustomerInfoEntity>();
		List<String> list = typeListMap.get(tag);
		for (String id : list) {
			if(cacheInformation.containsKey(id))infoEntities.add((CustomerInfoEntity)cacheInformation.get(id));
		}
		return infoEntities;
	}
	
	public List<String> getListByType(String type){
		synchronized (typeListMap) {
			return typeListMap.get(type);
		}
	}
	
	//最近参与的话题
	public List<String> getLatelyGroupIdList(){
		return getListByType("1");
	}
	
	//支付失败的话题
	public List<String> getbuyFailureGroupIdList(){
		return getListByType("11");
	}
	
	//我购买的话题
	public List<String> getMyBoughtGroupIdList(){
		return getListByType("12");
	}
	
	//我购买过的话题
	public List<String> getMyBoughtHisGroupIdList(){
		return getListByType("13");
	}
	
	//我关注的salon
	public List<String> getInterestGroupIdList(){
		return getListByType("2");
	}
	
	//创建的salon
	public List<String> getCreatedGroupIdList(){
		return getListByType("3");
	}
	
	//最近联系人
	public List<String> getLatelyFriendIdList(){
		return getListByType("4");
	}
	
	//最近联系医生
	public List<String> getLatelyDoctordList(){
		return getListByType("7");
	}
	//最近联系的客户
	public List<String> getLatelyCustomList(){
			return getListByType("6");
	}
	
	//我关注的好友
	public List<String> getInterestFriendIdList(){
		return getListByType("5");
	}
	
	/**
	 * 获得陌生人
	 * @return
	 */
	public List<String> getStrangerIdList(){
		return getListByType("14");
	}
	
	
	//我粉丝好友
/*	public List<String> getFansFriendIdList(){
		synchronized (typeListMap) {
			return typeListMap.get("6");
		}
	}*/
	
	//加入壹健康的微博好友
/*	public List<String> getWeibFriendIdList(){
		synchronized (typeListMap) {
			return typeListMap.get("7");
		}
	}*/
	
	//所有黑名单好友
	public List<String> getStrangerFriendIdList(){
		return getListByType("8");
	}
	
	/**
	 * 获得客户id列表
	 * @return
	 */
	public List<String> getCustomerIdList(){
		return getListByType("9");
	}
	
	/**
	 * 获得客户id列表
	 * @return
	 */
	public List<String> getDoctorIdList(){
		synchronized (typeListMap) {
			return typeListMap.get("10");
		}
	}
	
	/**
	 * 关注好友列表
	 * @return
	 */
	public List<CustomerInfoEntity> getInterestFriendList(){
		final List<CustomerInfoEntity> listEntities = new ArrayList<CustomerInfoEntity>();
		List<String> list = getInterestFriendIdList();
		for (int i = 0; i < list.size(); i++) {
			String id = list.get(i);
			if(cacheInformation.containsKey(id)){
				listEntities.add((CustomerInfoEntity)cacheInformation.get(id));
			}
		}
		return listEntities;
	}
	
	
	/**
	 * 更新最近参与的话题
	 * @param infoEntity
	 */
	public void updateLatelyGroup(GroupInfoEntity infoEntity){
		synchronized (typeListMap) {
			List<String> list = getLatelyGroupIdList();
			String id = infoEntity.getId();
			if(list.contains(id)){
				list.remove(id);
				list.add(0,id);
			}
		}
	}
	
	/**
	 * 更新最近聊天好友
	 * @param infoEntity
	 */
	public void updateLatelyFriend(CustomerInfoEntity infoEntity){
		String id = infoEntity.getId();
		synchronized (typeListMap) {
			List<String> list = getLatelyFriendIdList();
			if(list.contains(id)){
				list.remove(id);
				list.add(0,id);
			}
		}
	}
	
	/**
	 * 获取群资料
	 * @param id
	 * @return 没有资料返回null
	 */
	public GroupInfoEntity getGroupInfoEntity(String id){
		Object object = cacheInformation.get(id);
		if(object != null && object instanceof GroupInfoEntity){
			return (GroupInfoEntity)object;
		}
		return null;
	}
	
	/**
	 *好友资料 
	 * @param id
	 * @return
	 */
	public CustomerInfoEntity getCustomerInfoEntity(String id){
		Object object = cacheInformation.get(id);
		if(object instanceof CustomerInfoEntity){
			return (CustomerInfoEntity)object;
		}
		return null;
	}
	
	/**
	 * 添加关注
	 * @param entity
	 */
	public void addAttention(CustomerInfoEntity entity){
		String id = entity.getId();
		List<String> list = getInterestFriendIdList();
		if(!list.contains(id)){
			list.add(id);
		}
		cacheInformation.put(id,entity);
	}
	
	/**
	 * 
	 * 添加关注群
	 * @param entity
	 */
	public void addAttentionGroup(GroupInfoEntity entity){
		String id = entity.getId();
		List<String> list = getInterestGroupIdList();
		if(!list.contains(id)){
			list.add(id);
		}
		cacheInformation.put(id,entity);
	}
	
	/**
	 * 添加客户
	 * @param entity
	 */
	public void addCustomer(CustomerInfoEntity entity){
		String id = entity.getId();
		List<String> list = getCustomerIdList();
		if(!list.contains(id)){
			list.add(id);
		}
		cacheInformation.put(id,entity);
	}
	
	/**
	 * 获得客户列表
	 * @return
	 */
	public List<CustomerInfoEntity> getCustomerInfoList(){
		List<CustomerInfoEntity> infoEntities = new ArrayList<CustomerInfoEntity>();
		List<String> list = getCustomerIdList();
		for (String id : list) {
			if(cacheInformation.containsKey(id))infoEntities.add((CustomerInfoEntity)cacheInformation.get(id));
		}
		return infoEntities;
	}
	
	/**
	 * 获得关注的群
	 * @return
	 */
	public List<GroupInfoEntity> getInterestGroupInfoList(){
		List<GroupInfoEntity> infoEntities = new ArrayList<GroupInfoEntity>();
		List<String> list = getInterestGroupIdList();
		for (String id : list) {
			if(cacheInformation.containsKey(id))infoEntities.add((GroupInfoEntity)cacheInformation.get(id));
		}
		return infoEntities;
	}
	
	/**
	 * 更新资料
	 * @param infoEntity
	 * @param isUpdate 
	 */
	public void updateCacheInfomation(BaseInfoEntity infoEntity){
		synchronized (cacheInformation) {
			cacheInformation.put(infoEntity.getId(),infoEntity);
		}
	}
	
	/**
	 * 更新资料
	 * @param infoEntity
	 * @param isUpdate 
	 */
	public void removeCacheInfomation(BaseInfoEntity infoEntity){
		synchronized (cacheInformation) {
			cacheInformation.remove(infoEntity.getId());
		}
	}
	
	
	/**
	 * 更新最近联系组
	 * @param id
	 */
	public void updateLatelyGroupOrder(String id){
		List<String> list = getLatelyGroupIdList();
		list.remove(id);
		list.add(0,id);
	}
	
	/**
	 * 
	 * 未读消息数量
	 * @param id
	 * @return
	 */
	public int getNoReadMesgSize(String id){
		int size = 0;
		if(messageCllection.containsKey(id)){
			List<MessageEntity> list = messageCllection.get(id);
			size = list.size();
		}
		return size;
	}
	
	/**
	 * 获取salon未读的消息数量
	 */
	public int getSalonMesgCount(){
		List<String> salonMesgCount = getCreatedGroupIdList();//创建的salon
		salonMesgCount.addAll(getInterestGroupIdList());//关注的salon
		salonMesgCount.addAll(getbuyFailureGroupIdList());//购买失败的salon
		return getMesgCount(salonMesgCount);
	}
	
	/**
	 * 获取朋友未读的消息数量
	 */
	public int getFriendMesgCount(){
		List<String> mFriendMesgCount = getInterestFriendIdList();//创建的salon
		return getMesgCount(mFriendMesgCount);
	}
	
	/**
	 * 获取医生未读的消息数量
	 */
	public int getDoctorMesgCount(){
		return getMesgCount(getDoctorIdList());
	}
	
	/**
	 * 获取助理未读的消息数量
	 */
	public int getHelperMesgCount(){
		final String id = SmartFoxClient.helperId;
		if(messageCllection.containsKey(id)){
			 List<MessageEntity> list = messageCllection.get(id);
			 return list.size();
		}
		return 0;
	}
	
	
	/**
	 * 获得指定集合消息数量
	 * @param listMesgCount
	 * @return
	 */
	private int getMesgCount(List<String> listMesgCount) {
		int count = 0;
		for (String id : listMesgCount) {
			count += getNoReadMesgSize(id);
		}
		return count;
	}
	
	/**
	 * 0 助理未读消息
	 * 1 医生未读消息
	 * 2朋友未读消息
	 * 3话题未读消息
	 * @return
	 */
	public Map<Integer,Integer> getMesgTypeCount(){
		Map<Integer,Integer> map=new HashMap<Integer, Integer>();
		map.put(0, getHelperMesgCount());
		map.put(1, getDoctorMesgCount());
		map.put(2, getFriendMesgCount());
		map.put(3, getSalonMesgCount());
		return map;
	} 
	
}
